<?php

namespace App\Http\Controllers\Admin;

use App\Comment;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{
    public function index()
    {
        return view('admin/comment/index')->withComments(Comment::with('hasOneArticle')->get());
//        return view('admin/comment/index')->withComments(Comment::all());
    }

    public function edit($id)
    {
        $comment = Comment::with('hasOneArticle')->find($id);
        return view('admin/comment/edit')->with('comment', $comment);
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'content' => 'required'
        ]);
        $comment = Comment::find($id);
        $comment->content = $request->get('content');
        if ($comment->save()) {
            return redirect('admin/comment');
        } else {
            return redirect()->back()->withInput()->withErrors('修改评论失败');
        }

    }

    public function destroy($id)
    {
        Comment::find($id)->delete();
        return redirect()->back()->withInput()->withErrors('删除成功');
    }
}
